﻿using Microsoft.Windows.Controls.Ribbon;
using System.Windows.Controls;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Events;
using compshop.Infrastructure;
using System.Reflection;
using System.Collections.Generic;

namespace compshop.Desktop
{
    /// <summary>
    /// Interaction logic for MainWindow.xamlC:\Documents and Settings\Admin\мои документы\visual studio 2010\Projects\compshop.Desktop\Module2\myDataListener.cs
    /// </summary>
    public partial class Shell : RibbonWindow
    {
        private SubscriptionToken subscriptionToken;
        private IEventAggregator eventAggregator;

        public Shell()
        {
            InitializeComponent();
           
            foreach (FieldInfo fi in typeof(RegionNames).GetFields())
            {
                if (!fi.FieldType.Name.Contains("String")) return;
                ItemsControl ic = new ItemsControl();
                RegionManager.SetRegionName(ic, fi.GetValue(null).ToString());
                ic.Name = "item" + fi.GetValue(null).ToString();
                ic.Visibility = System.Windows.Visibility.Hidden;
                ClientArea.Children.Add(ic);                
            }

            eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            ShellChangeEvent shellChangeEvent = eventAggregator.GetEvent<ShellChangeEvent>();
            subscriptionToken = shellChangeEvent.Subscribe(this.ShowState, ThreadOption.UIThread, false);
        }

        public void ShowState(StateComposition listCreator)
        {           
            List<stateItem> list = listCreator();
            eventAggregator.GetEvent<ModuleActivityEvent>().Publish(new ModuleActivityState() { DeactivateAll = true });

            foreach (System.Windows.UIElement elem in ClientArea.Children)
            {
                if (!(elem is ItemsControl)) return;                
                bool found = false;
                foreach(stateItem si in list)
                {
                    if ((elem as ItemsControl).Name.Contains(si.RegionName))
                    {
                        found = true;
                        Grid.SetColumn(elem, si.Col);
                        Grid.SetColumnSpan(elem, si.ColSpan);
                        Grid.SetRow(elem, si.Row);
                        Grid.SetRowSpan(elem, si.RowSpan);
                        elem.Visibility = System.Windows.Visibility.Visible;
                        eventAggregator.GetEvent<ModuleActivityEvent>().Publish(new ModuleActivityState() { DeactivateAll = false, ActivateModule = si.RegionName, param = "*" });
                        break;
                    }
                }
                if (!found)
                {
                    elem.Visibility = System.Windows.Visibility.Hidden;
                }
            }

            list = null;
            //System.GC.Collect();
        }

        private void rbtnProductTree_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            eventAggregator.GetEvent<ShellChangeEvent>().Publish(ShellStates.GetProductCharachtersState);
        }

        private void rbtnOrders_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            eventAggregator.GetEvent<ShellChangeEvent>().Publish(ShellStates.GetOrderProductList);            
        }

        private void rbtnSupplList_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            eventAggregator.GetEvent<ShellChangeEvent>().Publish(ShellStates.GetSuppliersList);
        }

        private void rbtnSupplOrderList_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            eventAggregator.GetEvent<ShellChangeEvent>().Publish(ShellStates.GetOrderToSupplier);
        }

        private void rbtnRecievedGoods_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            eventAggregator.GetEvent<ShellChangeEvent>().Publish(ShellStates.GetReceivedGoods);
        }

        private void rbtnDepList_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            eventAggregator.GetEvent<ShellChangeEvent>().Publish(ShellStates.GetDepartmentList);
        }

        private void rbtnEmplList_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            eventAggregator.GetEvent<ShellChangeEvent>().Publish(ShellStates.GetEmployeeList);
        }

        private void rbtnPositionList_Click(object sender, System.Windows.RoutedEventArgs e)
        {

        }

        private void rbtnWaybills_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            eventAggregator.GetEvent<ShellChangeEvent>().Publish(ShellStates.GetWaybillsList);
        }

    }
}
